﻿using Microsoft.AspNetCore.Mvc;
using Qy.Models;
using Qy.Service;
using Qy.Web.Authorize;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Qy.Web.Api
{
    [Route("Api/[controller]/[action]")]
    [Verify]
    public class StatisticsController : Base
    {
        private readonly LogService _logService;
        private readonly LoginUtil _LoginUtil;
        private readonly NoticeKeyService _noticeKey;
        private readonly UserService _adminUserService;
        private readonly ContentService _contentService;
        public StatisticsController(
            LoginUtil LoginUtil,
            UserService adminUserService,
            ContentService contentService,
            LogService logService,
            NoticeKeyService noticeKeyService
            )
        {
            _LoginUtil = LoginUtil;
            _adminUserService = adminUserService;
            _contentService = contentService;
            _logService = logService;
            _noticeKey = noticeKeyService;
        }

        [HttpGet]
        public async Task<IActionResult> WorkspaceAsync(string contentCountMod = "")
        {
            int userId = _LoginUtil.LoginUserId();
            NumCount countAll = await _contentService.CountAllAsync();
            List<ContentCount> forUser = await _contentService.CountContentForUserAsync(contentCountMod);
            List<ViewLogSql> logList = await _logService.GetListAsync(userId);//显示3条

            return Json(new { status = 1, logList, forUser, countAll });
        }


        [HttpGet]
        public async Task<IActionResult> CountAsync()
        {
            int userId = _LoginUtil.LoginUserId();
            long notice = await _noticeKey.CountAsync(userId);//统计消息未读
            return Json(new { status = 1, data = notice });
        }

        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.Statistics_Index)]
        public async Task<IActionResult> ListAsync([FromQuery] StatisticsQuery query)
        {
            StatisticsQuery statisticsQuery = new()
            {
                SearchType = query.SearchType,
                SearchCid = query.SearchCid,
                NickName = query.NickName,
                StartDate = query.StartDate,
                EndDate = query.EndDate,
            };

            List<UserIdAndName> userIdAndName = await _adminUserService.GetNameAsync(0);
            var countList = await _contentService.StatisticsAsync(statisticsQuery);
            return Json(new { code = 0, data = countList, userIdAndName });
        }
    }
}
